package com.secondary.action.userAction;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.secondary.action.BaseAction;
import com.secondary.pojos.Category;
import com.secondary.pojos.Goods;
import com.secondary.pojos.User;
import com.secondary.service.CategoryService;
import com.secondary.service.GoodsService;
import com.secondary.service.UserService;
import com.secondary.util.MyImageUtil;

@ParentPackage(value = "authority")
@Namespace(value = "/changeuser")
@Action(value = "changeuser")
@Results( {
		
		@Result(name = "regSuccess", location = "/main/showIndex!show.action", type = "redirect"),
		@Result(name = "logSuccess", location = "/main/showIndex!show.action", type = "redirect"),	
		@Result(name = "gotoSave", location = "/user/saveGood.jsp"),
		@Result(name = "updateSuccess", location = "/user/userInfo.jsp"),
		@Result(name = "updateSuccess", location = "/user/userInfo.jsp"),
		@Result(name = "logFail", location = "/user/login.jsp"),
		@Result(name = "showUserInfoSuccess", location = "/user/userInfo.jsp"),
		@Result(name = "stateError", location = "/user/login.jsp") })
		
		
public class ChangeUserAction extends BaseAction {
	@Resource
	private UserService userService;
	@Resource
	private CategoryService categoryService;
	@Resource
	private GoodsService goodsService;
	
	private User user;	
	private File image;
	private String imageFileName;
	private String imageContentType;

	

	
	public String showUserInfo(){
		return "showUserInfoSuccess";
	}
	
	public String updateUserInfo() {
		
		userService.updateUserInfo(user);
		user = userService.findUserById(user.getId());
		super.setSessionAttribute("user", user);
		return "updateSuccess";
	}
	public String updateUserAvatar() {

		String realpath = ServletActionContext.getServletContext().getRealPath(
				"/user_avatar_" + user.getId());
		try {
			if (image != null) {
				File savefile = new File(new File(realpath), imageFileName);
				if (!savefile.getParentFile().exists())
					savefile.getParentFile().mkdirs();
				FileUtils.copyFile(image, savefile);
				user.setAvatar(imageFileName);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		userService.updateUserAvatar(user);
		user = userService.findUserById(user.getId());
		super.setSessionAttribute("user", user);
		return "updateSuccess";
	}

	public String checkUserPassWord() {

		String userPwd = userService.findUserPassWordById(Integer
				.parseInt(super.getRequestParameter("uid")));

		String pwd = super.getRequestParameter("pwd");
		JSONObject jsonObject = new JSONObject();
		if (userPwd.equals(pwd)) {

			jsonObject.accumulate("flag", "success");
			super.renderText(jsonObject.toString());
			System.out.println(jsonObject);
		} else {
			jsonObject.accumulate("flag", "fail");
			super.renderText(jsonObject.toString());
			System.out.println(jsonObject);
		}

		return null;
	}

	public String updateUserPassWord() {
		int id = Integer.parseInt(super.getRequestParameter("uid"));
		String passWord = super.getRequestParameter("pwd");
		userService.updateUserPassWord(id, passWord);
		user = userService.findUserById(id);
		super.setSessionAttribute("user", user);
		return "updateSuccess";
	}

	
	public String gotoLogin() {
		return "gotoLogin";
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}



	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}

	public GoodsService getGoodsService() {
		return goodsService;
	}
	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}

}
